from django.contrib import admin
from Post.models import Question, Choice


# 注册model
# 设置：每添加一个问题，附带俩个选项
class ChoiceInfo(admin.TabularInline):
    model = Choice
    extra = 2

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    # 修改描述
    def questionInfo(self):
        return self.question_text

    def dateInfo(self):
        return self.release_time

    questionInfo.short_description = "问题"
    dateInfo.short_description = "时间"
    # 列表页属性
    inlines = [ChoiceInfo]
    list_display = ['id', questionInfo, dateInfo]
    list_filter = ['question_text']
    search_fields = ['release_time']
    list_per_page = 5

    # 修改页属性，俩个值只能使用一个
    # fields 修改属性先后顺序
    # fieldsets 给属性分组
    fieldsets = [
        ("base",{"fields":['question_text']}),
        ("date",{"fields":['release_time']}),
    ]

#admin.site.register(Question, QuestionAdmin)

@admin.register(Choice)
class ChoiceAdmin(admin.ModelAdmin):
    # 修改描述
    def choiceInfo(self):
        return self.choice_text

    def voteInfo(self):
        return self.vote
    
    choiceInfo.short_description = "选项"
    voteInfo.short_description = "票数"
    # 列表页属性
    list_display = ['id', choiceInfo, voteInfo]
    list_filter = ['vote']
    search_fields = ['choice_text']
    list_per_page = 5

    fieldsets = [
        ("base",{"fields":['choice_text']}),
        ("num",{"fields":['vote']}),
    ]


#admin.site.register(Choice, ChoiceAdmin)